package com.xuhuan.thread.study2;

/**
 * @author huan.xu
 * @date 2021/9/29 15:13
 * @description 观测线程状态
 */
public class ObThreadDemo {

    public static void main(String[] args) {
        //创建线程
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程结束----------------");
            }

        });
        Thread.State state = thread.getState();
        System.out.println("线程状态：" + state);

        thread.start();

        while (state != Thread.State.TERMINATED) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            state = thread.getState();
            System.out.println("线程状态：" + state);
        }
    }
}
